#include "com_form_user.h"
#include "ui_com_form_user.h"

#include "pub_var.h"
#include <QSettings>
#include <QMessageBox>
#include <QDebug>
#include <QMenuBar>
#include <QHBoxLayout>
#include <QCheckBox>
#include <QSqlRecord>

//extern QMenuBar * mainmenu;
extern QStringList menuList;

com_Form_user::com_Form_user(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::com_Form_user)
{
    ui->setupUi(this);

    //恢复当前窗口位置
    QSettings setIni("clin.ini", QSettings::IniFormat);
    QRect rct = setIni.value("windowState/Form_user").toRect();
    this->setGeometry(rct);

    //数据库连接
    qrm = new QSqlQuery(pub::db_m);
    qro = new QSqlQuery(pub::db_o);
    qrot = new QSqlQuery(pub::db_o);
    mdUsers = new QSqlQueryModel();

    //定义角色的菜单项目
    ui->twMenu->clear();
    ui->twMenu->setColumnCount(3);
    ui->twMenu->setRowCount(menuList.size());

    QStringList titles;
    titles << "选择" << "功能菜单" << "menuid" << "parent";
    ui->twMenu->setHorizontalHeaderLabels(titles);
    ui->twMenu->setColumnHidden(2, true);
    ui->twMenu->setColumnHidden(3, true);

    QWidget *wgt;
    QHBoxLayout * lot;
    QCheckBox *ckbx;

    for(int row = 0; row < menuList.size(); row++)
    {
        wgt = new QWidget();
        lot = new QHBoxLayout();
        ckbx = new QCheckBox();
        lot->setMargin(1);
        lot->addWidget(ckbx, 0 ,Qt::AlignCenter);
        wgt->setLayout(lot);
        ui->twMenu->setCellWidget(row, 0, wgt);
        QString name = pub::getStr(menuList[row], "^", 1);
        QString id =  pub::getStr(menuList[row], "^", 2);
        ckbx->setProperty("menu_id", id);
        ckbx->setProperty("idx", row);
        connect(ckbx, &QCheckBox::clicked, this, &com_Form_user::listCheckBoxChanged);
        ui->twMenu->setItem(row,1,new QTableWidgetItem( name ));
        ui->twMenu->setItem(row,2,new QTableWidgetItem( id ));
    }

    flush(1);

    ui->lwToolBar->clear();
    qrm->exec( "select role_dept, name from com_privileges where type = 'tool_bar' ");

    while(qrm->next())
    {
        ui->lwToolBar->addItem(qrm->value(0).toString());
    }

    timer1 = new QTimer;
    connect(timer1, &QTimer::timeout, this, &com_Form_user::timer1out);

    ui->tabWidget->setCurrentIndex(0);

    ui->cbbZc->clear();
    qrm->exec("select name from com_dict_com where dic_type = '技术职称' order by ord");
    while(qrm->next())
    {
        ui->cbbZc->addItem(qrm->value(0).toString());
    }
    ui->cbbZc->setCurrentIndex(-1);

    ui->cbbZyfw->clear();
    qrm->exec("select name from com_dict_com where dic_type = '执业范围' order by ord");
    while(qrm->next())
    {
        ui->cbbZyfw->addItem(qrm->value(0).toString());
    }
    ui->cbbZyfw->setCurrentIndex(-1);

    timer1->start(500);
}

com_Form_user::~com_Form_user()
{
    delete ui;
}

void com_Form_user::timer1out()
{
    timer1->stop();
    int width = ui->tvUsers->width();
    ui->tvUsers->setColumnWidth(0, width * 0.4);
    ui->tvUsers->setColumnWidth(1, width * 0.4);

    width = ui->twMenu->width();
    ui->twMenu->setColumnWidth(0, width * 0.15);
    ui->twMenu->setColumnWidth(1, width * 0.65);

    width = ui->twRoles->width();
    ui->twRoles->setColumnWidth(0, width * 0.2);
    ui->twRoles->setColumnWidth(1, width * 0.55);

    width = ui->twDepartment->width();
    ui->twDepartment->setColumnWidth(0, width * 0.2);
    ui->twDepartment->setColumnWidth(1, width * 0.55);

}
void com_Form_user::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/Form_user", QVariant(this->geometry()));

    emit destroyW( "com_user" );

}

void com_Form_user::on_pushButton_clicked()
{


    close();

}

void com_Form_user::listCheckBoxChanged()
{

    QCheckBox *cb = qobject_cast<QCheckBox *>(sender());    //发出事件消息的checkBox
    QString menu_id = cb->property("menu_id").toString();   //菜单项id
    QString menu_text = pub::getStr(menuList[cb->property("idx").toInt()], "^", 1);     //菜单项文本

    QWidget *wgt;
    QHBoxLayout * lot;
    QCheckBox *ckbx;

    if(menu_text == menu_text.trimmed())    //menubar文本没有前导空格
    {
        //menubar
        for(int row = 0; row < menuList.size(); row++)
        {
            QString parentId = pub::getStr(menuList[row], "^", 3);
            if(parentId == menu_id) //同一menubar下的项目
            {
                wgt = ui->twMenu->cellWidget(row , 0);
                lot = qobject_cast<QHBoxLayout * > (wgt->layout());
                ckbx = qobject_cast<QCheckBox *>(lot->itemAt(0)->widget());
                ckbx->setChecked( cb->isChecked());     //设置选中状态与menubar相同
            }
        }
    }
    else
    {
        //menuItem
        bool v = false;
        for(int row = menuList.size() -1 ;  row >= 0 ; row--)
        {
            //逆序检索。同一menubar所有项目都没有选中，此menubar也设置未选中。只要menubar下有一个项目被选中，则些menubar设置选中。
            QString parentId = pub::getStr(menuList[row], "^", 3);
            if(parentId.isEmpty())
            {
                //无父节点，为menubar
                wgt = ui->twMenu->cellWidget(row, 0);
                lot = qobject_cast<QHBoxLayout * > (wgt->layout());
                ckbx = qobject_cast<QCheckBox *>(lot->itemAt(0)->widget());
                ckbx->setChecked( v );
                v = false;
            }else{
                //item
                wgt = ui->twMenu->cellWidget(row, 0);
                lot = qobject_cast<QHBoxLayout * > (wgt->layout());
                ckbx = qobject_cast<QCheckBox *>(lot->itemAt(0)->widget());
                v = v | ckbx->isChecked();  //累积checkbox状态
            }
        }
    }
}

void com_Form_user::on_pbRoleSave_clicked()
{
    QString roleName = ui->leRoleName->text().trimmed();
    if(roleName.isEmpty())
    {
        QMessageBox::warning(this, "注意", "角色名称不可以为空！");
        return;
    }
    int rows = ui->twMenu->rowCount();

    int function_count = 0;
    QWidget *wgt;
    QHBoxLayout * lot;
    QCheckBox *ckbx;

    //删除原有角色定义
    qrm->exec("delete from com_roles where role_name = '" + roleName + "'");

    for(int i = 0; i< rows; i++)
    {
        wgt = ui->twMenu->cellWidget(i,0);
        lot = qobject_cast<QHBoxLayout * > (wgt->layout());
        ckbx = qobject_cast<QCheckBox *>(lot->itemAt(0)->widget());

        if(ckbx->isChecked())
        {
            function_count++; //为角色选择的功能计数
            QString menu = ui->twMenu->item(i, 1)->text();
            QString id = ui->twMenu->item(i, 2)->text();
            sq = "insert into com_roles(menu_item,menu_id,role_name,oper_code,oper_date) ";
            sq += " values('" + menu.trimmed() + "','" + id + "','" + roleName + "','" + pub::usr_code + "', curdate() )";
            if(!qrm->exec(sq)){
                QMessageBox::warning(this, "注意", "保存角色发生错误！");
                qDebug()<<sq;
                return;
            }
        }
    }


    if(function_count < 1)
    {
        QMessageBox::warning(this, "注意", "至少应为角色选择一项可操作的功能！");
        return;
    }

    flush(1);
}

void com_Form_user::flush(int mode)
{
    ui->leRoleName->setText("");
    ui->leUserCode->setText("");
    ui->leUserName->setText("");
    ui->leYbysbm->setText("");
    ui->leZgzsbh->setText("");
    ui->le_senior_doct->setText("");
    ui->cbbZc->setCurrentIndex(-1);
    ui->cbbZyfw->setCurrentIndex(-1);

    QWidget *wgt;
    QHBoxLayout * lot;
    QCheckBox *ckbx;

    userList.clear();
    sq = "select  empl_code 账号, empl_name 姓名 , valid_state, si_code "
         " from com_employee ";
    if(ui->ckbValid->isChecked())
    {
        sq += " where valid_state = '1' order by convert(empl_name using gbk), empl_code ";
    }else{
        sq += " order by convert(empl_name using gbk), empl_code ";
    }

    mdUsers->setQuery(sq, pub::db_m);
    ui->tvUsers->setModel(mdUsers);
    ui->tvUsers->setColumnHidden(2, true);
    ui->tvUsers->setColumnHidden(3, true);

    for(int i = 0; i< mdUsers->rowCount(); i++)
    {
        userList << mdUsers->record(i).value(0).toString() + "^"
                    + mdUsers->record(i).value(1).toString() + "^"
                    + mdUsers->record(i).value(2).toString();
    }

    //菜单项
    for(int row = 0; row < menuList.size(); row++)
    {
        wgt = ui->twMenu->cellWidget(row , 0);
        lot = qobject_cast<QHBoxLayout * > (wgt->layout());
        ckbx = qobject_cast<QCheckBox *>(lot->itemAt(0)->widget());
        ckbx->setChecked( false);     //设置选中状态
    }

    //定义用户使用的角色列表
    for(int row = 0; row < roleList.size(); row++)
    {
        wgt = ui->twRoles->cellWidget(row , 0);
        lot = qobject_cast<QHBoxLayout * > (wgt->layout());
        ckbx = qobject_cast<QCheckBox *>(lot->itemAt(0)->widget());
        ckbx->setChecked( false);     //

    }

    //定义用户使用的诊室列表
    for(int row = 0; row < deptList.size(); row++)
    {
        wgt = ui->twDepartment->cellWidget(row , 0);
        lot = qobject_cast<QHBoxLayout * > (wgt->layout());
        ckbx = qobject_cast<QCheckBox *>(lot->itemAt(0)->widget());
        ckbx->setChecked( false);     //
    }

    if(mode == 0)
    {
        //不需要从数据库加载基础数据
        return;
    }

    QStringList titles;

    //定义角色的角色列表
    ui->lwRole->clear();
    roleList.clear();
    qrm->exec("select distinct role_name from com_roles order by convert(role_name using gbk)");
    while(qrm->next())
    {
        ui->lwRole->addItem(qrm->value(0).toString());
        roleList << qrm->value(0).toString();
    }

    //定义用户的角色列表
    ui->twRoles->clear();
    ui->twRoles->setColumnCount(2);
    ui->twRoles->setRowCount(roleList.size());

    titles.clear();
    titles << "选择" << "角色名称";
    ui->twRoles->setHorizontalHeaderLabels(titles);

    for(int row = 0; row < roleList.size(); row++)
    {
        wgt = new QWidget();
        lot = new QHBoxLayout();
        ckbx = new QCheckBox();
        lot->setMargin(1);
        lot->addWidget(ckbx, 0 ,Qt::AlignCenter);
        wgt->setLayout(lot);
        ui->twRoles->setCellWidget(row, 0, wgt);
        ui->twRoles->setItem(row,1,new QTableWidgetItem( roleList[row] ));
    }

    //定义用户的诊室列表
    deptList.clear();
    qrm->exec("select dept_name from com_department where valid_state = '1' order by convert(dept_name using gbk)");
    while(qrm->next())
    {
        deptList << qrm->value(0).toString();
    }
    ui->twDepartment->clear();
    ui->twDepartment->setColumnCount(2);
    ui->twDepartment->setRowCount(deptList.size());

    titles.clear();
    titles << "选择" << "关联诊室";
    ui->twDepartment->setHorizontalHeaderLabels(titles);

    for(int row = 0; row < deptList.size(); row++)
    {
        wgt = new QWidget();
        lot = new QHBoxLayout();
        ckbx = new QCheckBox();
        lot->setMargin(1);
        lot->addWidget(ckbx, 0 ,Qt::AlignCenter);
        wgt->setLayout(lot);
        ui->twDepartment->setCellWidget(row, 0, wgt);
        ui->twDepartment->setItem(row,1,new QTableWidgetItem( deptList[row] ));
    }

}


void com_Form_user::on_pbRoleRemove_clicked()
{
    QString roleName = ui->leRoleName->text().trimmed();
    QMessageBox:: StandardButton result =
            QMessageBox::warning(NULL, "注意", "是否删除角色名称“"+roleName+"”？", QMessageBox::Yes|QMessageBox::No);
    if(result != QMessageBox::Yes){
        return;
    }
    //
    qrm->exec("delete from com_roles where role_name = '" + roleName + "'");
    flush(1);

    //

}


void com_Form_user::on_lwRole_currentTextChanged(const QString &currentText)
{

    qrm->exec("select menu_id from com_roles where role_name = '"+currentText+"'");
    QString ids;
    //提取角色关联的id
    while(qrm->next()){
        ids += qrm->value(0).toString() + "^";
    }

    QWidget *wgt;
    QHBoxLayout * lot;
    QCheckBox *ckbx;

    for(int row = 0; row < menuList.size(); row++)
    {
        wgt = ui->twMenu->cellWidget(row , 0);
        lot = qobject_cast<QHBoxLayout * > (wgt->layout());
        ckbx = qobject_cast<QCheckBox *>(lot->itemAt(0)->widget());

        QString itemid = pub::getStr(menuList[row], "^", 2);

        //menuid或itemid与角色关联
        if(  ids.indexOf(itemid ) > -1 )
        {
            ckbx->setChecked(true);     //设置选中状态
        }else{
            ckbx->setChecked(false);     //设置选中状态
        }
    }

    ui->leRoleName->setText(currentText);
}

void com_Form_user::on_tabWidget_currentChanged(int index)
{
    Q_UNUSED(index)

    timer1out();//调整 listWidget 列宽

}

void com_Form_user::on_tvUsers_clicked(const QModelIndex &index)
{
    sq = "select name, code, role_dept, type from com_privileges where code = " + index.siblingAtColumn(0).data().toString();
    qrm->exec(sq);

    QString roles,depts;
    //提取角色关联的id
    while(qrm->next()){
        if(qrm->value(3).toString() == "role")
        {
            roles += qrm->value(2).toString() + "^";
        }else if(qrm->value(3).toString() == "dept"){
            depts += qrm->value(2).toString() + "^";
        }
    }

    QWidget *wgt;
    QHBoxLayout * lot;
    QCheckBox *ckbx;

    for(int row = 0; row < roleList.size(); row++)
    {
        wgt = ui->twRoles->cellWidget(row , 0);
        lot = qobject_cast<QHBoxLayout * > (wgt->layout());
        ckbx = qobject_cast<QCheckBox *>(lot->itemAt(0)->widget());

        //menuid或itemid与角色关联
        if( roles.indexOf(roleList[row] ) > -1 )
        {
            ckbx->setChecked(true);     //设置选中状态
        }else{
            ckbx->setChecked(false);     //设置选中状态
        }
    }

    for(int row = 0; row < deptList.size(); row++)
    {
        wgt = ui->twDepartment->cellWidget(row , 0);
        lot = qobject_cast<QHBoxLayout * > (wgt->layout());
        ckbx = qobject_cast<QCheckBox *>(lot->itemAt(0)->widget());

        //menuid或itemid与角色关联
        if(  depts.indexOf(deptList[row] ) > -1 )
        {
            ckbx->setChecked(true);     //设置选中状态
        }else{
            ckbx->setChecked(false);     //设置选中状态
        }
    }

    if(index.siblingAtColumn(2).data().toString() == "1")
    {
        ui->rbValid->setChecked(true);
    }else{
        ui->rbInvalid->setChecked(true);
    }
    ui->leUserName->setText(index.siblingAtColumn(1).data().toString());
    ui->leUserCode->setText(index.siblingAtColumn(0).data().toString());
    ui->leYbysbm->setText(index.siblingAtColumn(3).data().toString());

    //提取医师信息
    sq = "select si_code, 医师资格证书编号, zyfw, jszc, senior_doct "
         " from com_doctor_compare where empl_code = "
            + index.siblingAtColumn(0).data().toString();
    qro->exec(sq);
    if(qro->next())
    {
        QString jszc = pub::getDicN("技术职称", qro->value(3).toString());
        if(ui->leYbysbm->text().isEmpty())
        {
            ui->leYbysbm->setText(qro->value(0).toString());
        }
        //
        ui->leZgzsbh->setText(qro->value(1).toString());
        if(qro->value(2).toString().isEmpty())
        {
            ui->cbbZyfw->setCurrentIndex(-1);
        }
        else
        {
            ui->cbbZyfw->setCurrentText(qro->value(2).toString());
        }

        if(jszc.isEmpty())
        {
            ui->cbbZc->setCurrentIndex(-1);
        }else{
            ui->cbbZc->setCurrentText(jszc);
        }

        ui->le_senior_doct->setText(qro->value("senior_doct").toString());
    }
    else
    {
        if(ui->leYbysbm->text().isEmpty())
        {
            //ui->leYbysbm->setText("");
            ui->leZgzsbh->setText("");
            ui->cbbZyfw->setCurrentIndex(-1);
            ui->cbbZc->setCurrentIndex(-1);
            ui->le_senior_doct->setText("");
        }
    }

}

void com_Form_user::on_tvUsers_doubleClicked(const QModelIndex &index)
{
    on_tvUsers_clicked(index);
}

void com_Form_user::on_ckbValid_clicked()
{
    flush(0);
}

void com_Form_user::on_pbUserSave_clicked()
{
    QString userName = ui->leUserName->text().trimmed();
    QString userCode = ui->leUserCode->text().trimmed();
    if(userName.isEmpty())
    {
        QMessageBox::warning(this, "注意", "用户名称不可以为空！");
        return;
    }

    QWidget *wgt;
    QHBoxLayout * lot;
    QCheckBox *ckbx;

    sq = "select * from com_employee where empl_code = '"+userCode+"'";
    qrm->exec(sq);
    if(!qrm->next())  //新账户
    {
        QMessageBox:: StandardButton result;
        result = QMessageBox::warning(NULL, "注意", "用户账号无效，是否增加为新用户？",QMessageBox::Yes|QMessageBox::No);
        if(result == QMessageBox::No){
            return;
        }
        on_pbUserNew_clicked();
    }

    //删除原有用户定义
    qrm->exec("delete from com_privileges where type in('role', 'dept') and code = '" + userCode + "'");

    //保存用户角色
    for(int i = 0; i< roleList.size(); i++)
    {
        wgt = ui->twRoles->cellWidget(i,0);
        lot = qobject_cast<QHBoxLayout * > (wgt->layout());
        ckbx = qobject_cast<QCheckBox *>(lot->itemAt(0)->widget());

        if(ckbx->isChecked())
        {
            sq = "insert into com_privileges (type, name, code, role_dept, oper_code, oper_date) ";
            sq += " values('role','" + userName + "'," + userCode + ",'" + ui->twRoles->item(i, 1)->text() ;
            sq += "','" + pub::usr_code + "', curdate() )";

            if(!qrm->exec(sq)){
                QMessageBox::warning(this, "注意", "保存角色发生错误！");
                qDebug()<<sq;
                return;
            }
        }
    }

    //保存用户诊室
    for(int i = 0; i< deptList.size(); i++)
    {
        wgt = ui->twDepartment->cellWidget(i,0);
        lot = qobject_cast<QHBoxLayout * > (wgt->layout());
        ckbx = qobject_cast<QCheckBox *>(lot->itemAt(0)->widget());

        if(ckbx->isChecked())
        {
            sq = "insert into com_privileges (type, name, code, role_dept, oper_code, oper_date) ";
            sq += " values('dept','" + userName + "'," + userCode + ",'" + ui->twDepartment->item(i, 1)->text() ;
            sq += "','" + pub::usr_code + "', curdate() )";

            if(!qrm->exec(sq)){
                QMessageBox::warning(this, "注意", "保存诊室发生错误！");
                qDebug()<<sq;
                return;
            }
        }
    }
    QString state =  (ui->rbValid->isChecked() ? "1" : "0") ;
    sq = " update com_employee set "
         " valid_state = '%1' , "
         " si_code = '%2' "
         " where empl_code = '%3'";

    sq  = sq.arg(state)
            .arg(ui->leYbysbm->text().trimmed())
            .arg(userCode);

    qrm->exec(sq);

    //医师编码非空，保存医师信息

    if(ui->leYbysbm->text().trimmed().isEmpty()
            && ui->leZgzsbh->text().trimmed().isEmpty()
            )
    {
        //医师编码和资格证书号同时为空
    }
    else if(!ui->leYbysbm->text().trimmed().isEmpty()
            && !ui->leZgzsbh->text().trimmed().isEmpty()
            && !ui->cbbZc->currentText().isEmpty())
    {
        sq = "update com_doctor_compare set ";
        sq += " si_code = '" + ui->leYbysbm->text().trimmed() + "',";
        sq += " 医师资格证书编号 = '" + ui->leZgzsbh->text().trimmed() + "',";
        sq += " zyfw = '" + ui->cbbZyfw->currentText().trimmed() + "',";
        sq += " jszc = '" + pub::getDicC("技术职称",ui->cbbZc->currentText().trimmed()) + "',";
        sq += " senior_doct = '" + ui->le_senior_doct->text().trimmed() + "'";
        sq += " where empl_code = '" + userCode + "' ";
        qro->exec(sq);
        qDebug()<<"变更医师信息："<<sq;
        if(qro->numRowsAffected() == 0)
        {
            qDebug()<<"com_doctor_compare update failed, now try to insert.";

            sq = "insert into com_doctor_compare (empl_code, empl_name, valied_state, "
                 " si_code, 医师资格证书编号, zyfw, jszc, senior_doct) "
                    "values('" + userCode + "','" + userName + "','1','";
            sq += ui->leYbysbm->text().trimmed() + "','";
            sq += ui->leZgzsbh->text().trimmed() + "','";
            sq += ui->cbbZyfw->currentText().trimmed() + "','";
            sq += pub::getDicC("技术职称",ui->cbbZc->currentText().trimmed())  + "',";
            sq += "'" + ui->le_senior_doct->text().trimmed() + "'";
            sq += " ) ";

            if(!qro->exec(sq))
            {
                QMessageBox::warning(NULL, "注意", "增加医师信息失败！");
                qDebug()<<"com_doctor_compare insert return error.";
                qDebug()<<sq;
                return;
            }
        }
    }
    else
    {   //医师编码和资格证书号空或非空不一致
        //QMessageBox::warning(NULL, "注意", "医保编码、资格证书号、技术职称必须同时为空或同时非空！");
        //return;
    }

    flush(0);
}

void com_Form_user::on_pbUserNew_clicked()
{
    QString userName = ui->leUserName->text().trimmed();
    if(userName.isEmpty())
    {
        QMessageBox::information(this,"注意", "用户姓名不可以为空！");
        return;
    }
    qrm->exec("select empl_name from com_employee where empl_name = '"+userName+"'");
    if(qrm->next())
    {
        QMessageBox::StandardButton result;
        result = QMessageBox::warning(this, "注意","姓名“" + userName + "”已经有账号，是否为些姓名分配新账号？",QMessageBox::Yes|QMessageBox::No);

        if(result == QMessageBox::No)
        {
            return;
        }
    }
    qrm->exec("select max(empl_code) + 1 from com_employee");
    qrm->next();
    QString t = qrm->value(0).toString();

    QString userCode = QString("000000");

    userCode.replace(6 - t.length(), t.length(), t);

    sq = "insert into com_employee"
         "(pswd, empl_code, empl_name, si_code, valid_state, oper_code, oper_date) "
         " values (";
    sq += "md5('0') ,'"
            + userCode
            + "','"
            + userName
            + "','"
            + ui->leYbysbm->text().trimmed()
            + "','1','"
            + pub::usr_code
            + "',curdate()";
    sq += ")";
    if(!qrm->exec(sq)){
        QMessageBox::warning(NULL, "注意", "mysql 增加新用户失败！");
        qDebug()<<sq;
        return;
    }

    sq = "insert into com_employee"
         "( empl_code, empl_name, oper_code, oper_date) "
         " values ('";
    sq += userCode + "','" + userName + "','" + pub::usr_code + "', sysdate";
    sq += ")";

    if(!qro->exec(sq)){
        QMessageBox::warning(NULL, "注意", "oracle 增加新用户失败！");
        qDebug()<<sq;
        return;
    }

    //医师编码非空，保存医师信息
    if(!ui->leYbysbm->text().trimmed().isEmpty()
            && !ui->leZgzsbh->text().trimmed().isEmpty()
            && !ui->cbbZc->currentText().isEmpty())
    {
        sq = "update com_doctor_compare set ";
        sq += " si_code = '" + ui->leYbysbm->text().trimmed() + "',";
        sq += " 医师资格证书编号 = '" + ui->leZgzsbh->text().trimmed() + "',";
        sq += " zyfw = '" + ui->cbbZyfw->currentText().trimmed() + "',";
        sq += " jszc = '" + pub::getDicC("技术职称",ui->cbbZc->currentText().trimmed())  + "',";
        sq += " senior_doct = '" + ui->le_senior_doct->text().trimmed() + "'";
        sq += " where empl_code = '" + userCode + "' ";
        qro->exec(sq);
        qDebug()<<"新增医师，变更："<<sq;
        if(qro->numRowsAffected() == 0)
        {
            qDebug()<<"com_doctor_compare update failed, now try to insert.";

            sq = "insert into com_doctor_compare (empl_code, si_code, 医师资格证书编号, "
                 " zyfw, jszc,senior_doct) "
                    "values('" + userCode + "','";
            sq += ui->leYbysbm->text().trimmed() + "','";
            sq += ui->leZgzsbh->text().trimmed() + "','";
            sq += ui->cbbZyfw->currentText().trimmed() + "','";
            sq += pub::getDicC("技术职称",ui->cbbZc->currentText().trimmed()) + "',";
            sq += "'" + ui->le_senior_doct->text().trimmed() + "'";
            sq += " ) ";
            qDebug()<<"新增医师，插入："<<sq;
            if(!qro->exec(sq))
            {
                QMessageBox::warning(NULL, "注意", "增加医师信息失败！");
                qDebug()<<"com_doctor_compare insert return error.";
                qDebug()<<sq;
                return;
            }
        }
    }

    flush(0);
}

void com_Form_user::on_tbAdd_clicked()
{
    if( ! ui->twMenu->currentIndex().isValid())
    {
        QMessageBox::warning(NULL, "注意", "未选定有效菜单项目！");
        return;
    }

    QString item_name = ui->twMenu->currentIndex().siblingAtColumn(1).data().toString();
    QString item_id = ui->twMenu->currentIndex().siblingAtColumn(2).data().toString();
    if(item_name.length() == item_name.trimmed().length())
    {
        QMessageBox::warning(NULL, "注意", "只可以选择菜单明细项目！");
        return;
    }
    QList<QListWidgetItem*> find = ui->lwToolBar->findItems(item_name.trimmed(), Qt::MatchExactly);
    if(find.count() > 0)
    {
        QMessageBox::warning(NULL, "注意", "不可以选择重复项目！");
        return;
    }
    ui->lwToolBar->addItem(item_name.trimmed());
    sq = "insert into com_privileges (type, role_dept, name) values ('tool_bar','" + item_name.trimmed() + "', '" + item_id + "')";

    qrm->exec(sq);

}


void com_Form_user::on_tbRemove_clicked()
{
    if( ! ui->lwToolBar->currentIndex().isValid())
    {
        QMessageBox::warning(NULL, "注意", "未选定有效工具栏项目！");
    }
    else
    {
        QString  t=ui->lwToolBar->currentItem()->text();
        ui->lwToolBar->takeItem(ui->lwToolBar->row(ui->lwToolBar->currentItem()));

        qrm->exec("delete from  com_privileges where type = 'tool_bar' and role_dept = '" + t + "'");
    }
}
